﻿Imports System.IO

Public Class CollectionTypesProvider
    Inherits TypeProvider
    Public Overrides ReadOnly Property Types As Type()
        Get
            Return {GetType(IEnumerable)}
        End Get
    End Property
    Public Overrides Function ReadObject(T As Type) As Object
        Dim lx As New List(Of Object)
        Info.AddCache(lx)
        While Reader.Peek <> -1
            Dim Ch = Chr(Reader.Peek)
            Using OX As New StateReader(Info)
                lx.Add(OX.DeserializeInternal(Reader))
            End Using
            Ch = Chr(Reader.Read)
            While Not TypeProvider.IsValidChar(Ch)
                Ch = Chr(Reader.Read)
            End While
            If Ch = Info.EndBlock Then
                Exit While
            End If
        End While
        Return CType(lx, IEnumerable)
    End Function
    Public Overrides Sub WriteObject(Obj As Object)
        Dim s As String = ""
        Info.AddCache(Obj)
        For Each x As Object In CType(Obj, IEnumerable)
            Write(s)
            Using Sw As New StateWriter(Info)
                Sw.SerializeInternal(x, Writer)
            End Using
            s = Info.Collection
        Next
    End Sub
End Class